<?php

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// phpBBnews
// =========
//
// Copyright (C) 2007 mgsninja
// Some functions are taken from phpBB 2 and are copyright (C) 2001 The phpBB Group
// The changes of these are copyright (C) 2007 mgsninja
//
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
////////////////////////////////////////////////////////////////////////////////////////////////////


/**
 * BBCode functions
 */
function load_bbcode_template()
{
	global $pbn;

	$tpl = $pbn['BBCode_tpl'];

	// replace \ with \\ and then ' with \'.
	$tpl = str_replace('\\', '\\\\', $tpl);
	$tpl  = str_replace('\'', '\\\'', $tpl);

	// strip newlines.
	$tpl  = str_replace("\n", '', $tpl);

	// Turn template blocks into PHP assignment statements for the values of $bbcode_tpls..
	$tpl = preg_replace('#<!-- BEGIN (.*?) -->(.*?)<!-- END (.*?) -->#', "\n" . '$bbcode_tpls[\'\\1\'] = \'\\2\';', $tpl);
	$bbcode_tpls = array();

	eval($tpl);

	return $bbcode_tpls;
}

function prepare_bbcode_template($bbcode_tpl)
{
	global $pbn;

	$bbcode_tpl['olist_open'] = str_replace('{LIST_TYPE}', '\\1', $bbcode_tpl['olist_open']);

	$bbcode_tpl['color_open'] = str_replace('{COLOR}', '\\1', $bbcode_tpl['color_open']);

	$bbcode_tpl['size_open'] = str_replace('{SIZE}', '\\1', $bbcode_tpl['size_open']);

	$bbcode_tpl['quote_open'] = str_replace('{L_QUOTE}', $pbn['News_Quote'], $bbcode_tpl['quote_open']);

	$bbcode_tpl['quote_username_open'] = str_replace('{L_QUOTE}', $pbn['News_Quote'], $bbcode_tpl['quote_username_open']);
	$bbcode_tpl['quote_username_open'] = str_replace('{L_WROTE}', $pbn['News_Wrote'], $bbcode_tpl['quote_username_open']);
	$bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $bbcode_tpl['quote_username_open']);

	$bbcode_tpl['code_open'] = str_replace('{L_CODE}', $pbn['News_Code'], $bbcode_tpl['code_open']);

	$bbcode_tpl['img'] = str_replace('{URL}', '\\1', $bbcode_tpl['img']);

	// We do URLs in several different ways..
	$bbcode_tpl['url1'] = str_replace('{URL}', '\1\2', $bbcode_tpl['url']);
	$bbcode_tpl['url1'] = str_replace('{DESCRIPTION}', '\1\2', $bbcode_tpl['url1']);

	$bbcode_tpl['url2'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
	$bbcode_tpl['url2'] = str_replace('{DESCRIPTION}', '\\1', $bbcode_tpl['url2']);

	$bbcode_tpl['url3'] = str_replace('{URL}', '\\1\\2', $bbcode_tpl['url']);
	$bbcode_tpl['url3'] = str_replace('{DESCRIPTION}', '\\6', $bbcode_tpl['url3']);

	$bbcode_tpl['url4'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
	$bbcode_tpl['url4'] = str_replace('{DESCRIPTION}', '\\5', $bbcode_tpl['url4']);

	$bbcode_tpl['email'] = str_replace('{EMAIL}', '\\1', $bbcode_tpl['email']);

	define("BBCODE_TPL_READY", true);

	return $bbcode_tpl;
}

function bbencode($text, $uid)
{
	global $pbn, $bbcode_tpl;

	// pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
	// This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
	$text = " " . $text;

	// First: If there isn't a "[" and a "]" in the message, don't bother.
	if (! (strpos($text, "[") && strpos($text, "]")) )
	{
		// Remove padding, return.
		$text = substr($text, 1);
		return $text;
	}

	// Only load the templates ONCE..
	if (!defined("BBCODE_TPL_READY"))
	{
		// load templates from file into array.
		$bbcode_tpl = load_bbcode_template();

		// prepare array for use in regexps.
		$bbcode_tpl = prepare_bbcode_template($bbcode_tpl);
	}

	// [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
	if($pbn['BBCode_code'] == 'On')
	{
		$text = bbencode_second_pass_code($text, $uid, $bbcode_tpl);
	}

	// [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
	if($pbn['BBCode_quote'] == 'On')
	{
		$text = str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text);
		$text = str_replace("[/quote:$uid]", $bbcode_tpl['quote_close'], $text);

		// New one liner to deal with opening quotes with usernames...
		// replaces the two line version that I had here before..
		$text = preg_replace("/\[quote:$uid=\"(.*?)\"\]/si", $bbcode_tpl['quote_username_open'], $text);
	}

	// [list] and [list=x] for (un)ordered lists.
	// unordered lists
	if($pbn['BBCode_list'] == 'On')
	{
		$text = str_replace("[list:$uid]", $bbcode_tpl['ulist_open'], $text);
		// li tags
		$text = str_replace("[*:$uid]", $bbcode_tpl['listitem'], $text);
		// ending tags
		$text = str_replace("[/list:u:$uid]", $bbcode_tpl['ulist_close'], $text);
		$text = str_replace("[/list:o:$uid]", $bbcode_tpl['olist_close'], $text);
		// Ordered lists
		$text = preg_replace("/\[list=([a1]):$uid\]/si", $bbcode_tpl['olist_open'], $text);
	}

	// colours
	if($pbn['BBCode_color'] == 'On')
	{
		$text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", $bbcode_tpl['color_open'], $text);
		$text = str_replace("[/color:$uid]", $bbcode_tpl['color_close'], $text);
	}

	// size
	if($pbn['BBCode_size'] == 'On')
	{
		$text = preg_replace("/\[size=([1-2]?[0-9]):$uid\]/si", $bbcode_tpl['size_open'], $text);
		$text = str_replace("[/size:$uid]", $bbcode_tpl['size_close'], $text);
	}

	// [b] and [/b] for bolding text.
	if($pbn['BBCode_b'] == 'On')
	{
		$text = str_replace("[b:$uid]", $bbcode_tpl['b_open'], $text);
		$text = str_replace("[/b:$uid]", $bbcode_tpl['b_close'], $text);
	}

	// [u] and [/u] for underlining text.
	if($pbn['BBCode_u'] == 'On')
	{
		$text = str_replace("[u:$uid]", $bbcode_tpl['u_open'], $text);
		$text = str_replace("[/u:$uid]", $bbcode_tpl['u_close'], $text);
	}

	// [i] and [/i] for italicizing text.
	if($pbn['BBCode_i'] == 'On')
	{
		$text = str_replace("[i:$uid]", $bbcode_tpl['i_open'], $text);
		$text = str_replace("[/i:$uid]", $bbcode_tpl['i_close'], $text);
	}

	// Patterns and replacements for URL and email tags..
	$patterns = array();
	$replacements = array();

	// [img]image_url_here[/img] code..
	// This one gets first-passed..
	if($pbn['BBCode_img'] == 'On')
	{
		$patterns[] = "#\[img:$uid\](.*?)\[/img:$uid\]#si";
		$replacements[] = $bbcode_tpl['img'];
	}

	// [url]xxxx://www.phpbb.com[/url] code..
	if($pbn['BBCode_url'] == 'On')
	{
		$patterns[] = "#\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]#is";
		$replacements[] = $bbcode_tpl['url1'];

		// [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).
		$patterns[] = "#\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]#si";
		$replacements[] = $bbcode_tpl['url2'];

		// [url=xxxx://www.phpbb.com]phpBB[/url] code..
		$patterns[] = "#\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]#si";
		$replacements[] = $bbcode_tpl['url3'];

		// [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).
		$patterns[] = "#\[url=(([\w\-]+\.)*?[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\](.*?)\[/url\]#si";
		$replacements[] = $bbcode_tpl['url4'];
	}

	// [email]user@domain.tld[/email] code..
	if($pbn['BBCode_email'] == 'On')
	{
		$patterns[] = "#\[email\]([a-z0-9\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
		$replacements[] = $bbcode_tpl['email'];
	}

	$text = preg_replace($patterns, $replacements, $text);

	// Remove our padding from the string..
	$text = substr($text, 1);

	return $text;

} // bbencode_second_pass()

function bbencode_second_pass_code($text, $uid, $bbcode_tpl)
{
	global $pbn;

	$code_start_html = $bbcode_tpl['code_open'];
	$code_end_html =  $bbcode_tpl['code_close'];

	// First, do all the 1st-level matches. These need an htmlspecialchars() run,
	// so they have to be handled differently.
	$match_count = preg_match_all("#\[code:1:$uid\](.*?)\[/code:1:$uid\]#si", $text, $matches);

	for ($i = 0; $i < $match_count; $i++)
	{
		$before_replace = $matches[1][$i];
		$after_replace = $matches[1][$i];

		// Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.
		$after_replace = str_replace("  ", "&nbsp; ", $after_replace);
		// now Replace 2 spaces with " &nbsp;" to catch odd #s of spaces.
		$after_replace = str_replace("  ", " &nbsp;", $after_replace);

		// Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.
		$after_replace = str_replace("\t", "&nbsp; &nbsp;", $after_replace);

		$str_to_match = "[code:1:$uid]" . $before_replace . "[/code:1:$uid]";

		$replacement = $code_start_html;
		$replacement .= $after_replace;
		$replacement .= $code_end_html;

		$text = str_replace($str_to_match, $replacement, $text);
	}

	// Now, do all the non-first-level matches. These are simple.
	$text = str_replace("[code:$uid]", $code_start_html, $text);
	$text = str_replace("[/code:$uid]", $code_end_html, $text);

	return $text;

} // bbencode_second_pass_code()

function smilies($message)
{
	global $pbn;

	$message = $message.' ';

	$sql = 'SELECT * FROM '.$pbn['DB_TablePrefix'].'config WHERE config_name = "smilies_path"';
	$result = mysql_query($sql);
	$board_config = mysql_fetch_array($result);

	$sql = 'SELECT code, smile_url FROM '.$pbn['DB_TablePrefix'].'smilies';
	$result = mysql_query($sql);
	while($smilies = mysql_fetch_array($result))
	{
		$message = str_replace(' '.$smilies['code'].' ', ' <img src="./'. $pbn['General_phpBB2Path'].$board_config['config_value'] . '/' . $smilies['smile_url'] . '" border="0" /> ', $message);
		$message = str_replace($smilies['code'].' ', ' <img src="./'. $pbn['General_phpBB2Path'].$board_config['config_value'] . '/' . $smilies['smile_url'] . '" border="0" /> ', $message);
		$message = str_replace(' '.$smilies['code'], ' <img src="./'. $pbn['General_phpBB2Path'].$board_config['config_value'] . '/' . $smilies['smile_url'] . '" border="0" /> ', $message);
		$message = str_replace($smilies['code'], ' <img src="./'. $pbn['General_phpBB2Path'].$board_config['config_value'] . '/' . $smilies['smile_url'] . '" border="0" /> ', $message);
	}

	return $message;
}

function smiliesRemove($message)
{
	global $pbn;

	$message = $message.' ';

	$sql = 'SELECT * FROM '.$pbn['DB_TablePrefix'].'config WHERE config_name = "smilies_path"';
	$result = mysql_query($sql);
	$board_config = mysql_fetch_array($result);

	$sql = 'SELECT code, smile_url FROM '.$pbn['DB_TablePrefix'].'smilies';
	$result = mysql_query($sql);
	while($smilies = mysql_fetch_array($result))
	{
		$message = str_replace(' '.$smilies['code'].' ', '', $message);
		$message = str_replace($smilies['code'].' ', '', $message);
		$message = str_replace(' '.$smilies['code'], '', $message);
		$message = str_replace($smilies['code'], '', $message);
	}

	return $message;
}

?>